
var vue;
function vue_init() {
    vue = new Vue({
        delimiters:['{!', '!}'],
        el: "#market-app",
        data: {
            CoinDetail: {},
            // flag: false
            coin_type:"",
            price_btc: "",
            price_rmb: "",
        },
        methods:{
            init:function () {
                var params = get_url_params()
                // console.log(" get coin_type " + params['coin_type'])
                // console.log(params.coin_type);
                this.get_coin_detail(params.coin_type);
                this.coin_type = params.coin_type;
            },
			to_deposit:function(){
                if (this.CoinDetail.deposit_open){
                    window.location.href = './deposit?coin_type='+ this.coin_type;
                }
			},
			to_withdrawal:function(){
                if(this.CoinDetail.withdraw_open){
                    window.location.href = './withdrawal?coin_type='+ this.coin_type;
                }
				
            },
            /* 根据类型获取数据 */
            get_coin_detail: function (coin_type) {
                // console.log(123);
                /* 发送请求 */
                $.get("/m/get_balance", {
                    coin_type: coin_type
                }, (data) => {
                    /* 获取数据，赋值 */
                    // console.log(data);
                    this.CoinDetail = data
                    /* 图标类型 */
                    var coin_info = get_coin_info(data.coin_type)
                    // console.log(coin_info)
                    /* 图标样式 */
                    this.CoinDetail.img = get_coin_icon_url(data.coin_type)
                    // console.log("["+detail.img+"]")
                    this.CoinDetail.amount = data.amount;
                    this.CoinDetail.name_upper = coin_info.name_upper;
                    this.CoinDetail.full_name = coin_info.cname;
                    // console.log(this.CoinDetail.trade_list.length);
                    /* 判断是否显示交易模块 */
                    if(!this.CoinDetail.trade_list.length){
                        /* 不显示 */
                        // console.log(123);
                        // this.flag = true;
                        // console.log($(".tradeMarkets"));
                        $(".tradeMarkets").css("display", "none");
                        $(".noTrade").css("display", "block");
                    }else{
                        // console.log(987);
                        /* 显示 */
                        for ( i = 0; i < this.CoinDetail.trade_list.length; i++) {
                            /* 定义一个变量 */
                            var item = this.CoinDetail.trade_list[i]
                            /* 装换大小写 */
                            var info = get_coin_info(item.coin_type);
                            item.coin_type_upper = info.name_upper

                            var info = get_coin_info(item.use_coin_type);  
                            item.use_coin_type_upper = info.name_upper
                        }
                    }
                    
                    // console.log(this.CoinDetail);

                    /* 判断按钮是否高亮显示 */
                    if(!this.CoinDetail.deposit_open){
                        // console.log(this.CoinDetail.deposit_open);
                        $(".deposit-btn").css("backgroundColor", "#92a0ad");
                        // console.log(2222222);
                    }else{}
                    if(!this.CoinDetail.withdraw_open){
                        // console.log(1111111);
                        $(".withdraw-btn").css("backgroundColor", "#92a0ad");
                    }else{}

                    /* 调用函数获取估值 */
                    this.get_coin_price(this.CoinDetail.coin_type);
                })
                
            },

            /* 跳转到历史记录页面 */
            to_history: function (coin_type) { 
                window.location.href = './history?coin_type=' + coin_type;
            },

            /* 获取估值部分 */
            get_coin_price: function (coin_type) {
                $.post("/btc/get_coin_price", {}, (res) => {
                    // console.log(res);
                    if(res[coin_type]){
                        // console.log(this.CoinDetail.amount);
                        this.price_rmb += this.CoinDetail.amount * res[coin_type];
                        this.price_rmb = parseInt(this.price_rmb) || 0;
                        /* 保留八位小数 */
                        this.price_btc = (this.price_rmb / res.btc).toFixed(8) || 0;
                    }else{
                        this.price_rmb = 0;
                        this.price_btc = (0).toFixed(8);
                    }
                });
            }
        }

    });
    vue.init();
}

$(function () {
    vue_init();
})